package com.example.act6.study.controller;


import com.example.act6.common.entities.CommonResult;
import com.example.act6.study.service.ActivitiTaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @return {@link null}
 * @Author jrw
 * @Description 任务相关控制器
 * @Date 2022/3/9 10:31
 * @Param
 **/
@RestController
@RequestMapping("/task")
public class ActivitTaskController {

    @Autowired
    private ActivitiTaskService activitiTaskService;

    /**
     * 获得待签事务列表
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/claimList")
    public CommonResult claimList(@RequestParam Map<String, Object> params) {
        List<Task> list = activitiTaskService.querySignPage(params);
        List<Map<String, Object>> mapList = list.stream().map(task -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", task.getId());
            map.put("createTime", task.getCreateTime());
            map.put("name", task.getName());
            map.put("category", task.getCategory());
            return map;
        }).collect(Collectors.toList());
        return CommonResult.ok(mapList);
    }

    /**
     * 获得待办列表
     *
     * @param params
     * @return
     */
    @PostMapping("/todoList")
    public CommonResult todoList(@RequestParam Map<String, Object> params) {
        List<Task> list = activitiTaskService.queryTodoPage(params);
        List<Map<String, Object>> mapList = list.stream().map(task -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", task.getId());
            map.put("createTime", task.getCreateTime());
            map.put("name", task.getName());
            map.put("category", task.getCategory());
            return map;
        }).collect(Collectors.toList());
        return CommonResult.ok(mapList);
    }

    /**
     * 获得已发事务列表
     *
     * @param params
     * @return
     */
    @PostMapping("/sentList")
    public CommonResult sentList(@RequestParam Map<String, Object> params) {
        return CommonResult.ok(activitiTaskService.querySentPage(params));
    }

    /**
     * 查看登陆用户 参与过的办结任务
     *
     * @param params
     * @return
     */
    @PostMapping("/finishList")
    public CommonResult finishList(@RequestParam Map<String, Object> params) {
        return CommonResult.ok(activitiTaskService.queryFinishPage(params));
    }

    /**
     * 登陆用户 签收任务
     *
     * @param taskId   任务id
     * @param assignee 当前用户
     * @return
     */
    @PostMapping("/claim")
    public CommonResult claim(String taskId, String assignee) {
        return activitiTaskService.claim(taskId, assignee);
    }

    /**
     * 获得对应任务的所有批注
     *
     * @param taskId 任务id
     * @return
     */
    @GetMapping("/commentList")
    public CommonResult commentList(String taskId) {
        return CommonResult.ok(activitiTaskService.queryCommentPage(taskId));
    }

    /**
     * 登陆用户 完成任务
     *
     * @param taskId 任务id
     * @param params 包含：当前用户 跟 批注
     * @return
     */
    @PostMapping("/complete")
    public CommonResult complete(String taskId, @RequestParam Map<String, Object> params) {
        return activitiTaskService.complete(taskId, params);
    }

    /**
     * 根据任务id 获得对应的流程图 包含流程记录路线
     *
     * @param processInstanceId 任务id
     * @param response
     * @return
     */
    @PostMapping("/viewProgressPathImage")
    public void viewProgressPathImage(String processInstanceId, HttpServletResponse response) {
        activitiTaskService.getActivitiProccessImage(processInstanceId, response);
    }
}
